<?php
namespace App\Models\Promotion;

use DB;

use App\Models\Goods\GoodsSale;

use App\Models\Goods\GoodsStore;

use App\Models\Goods\GoodsMarktingStrategy;

use App\Models\ConfigureFunction;

use Illuminate\Database\Eloquent\Model;

class PromotionOrder extends Model
{

    protected $table = 'promotion_order';

    protected $primaryKey = 'promotion_id';

    public $timestamps = false;

    public $incrementing = false;


    /**
     * 检查商品促销
     */
    public static function search( $arg_array )
    {

        //sql字符串
        $sql = '';

        //分类sql
        $category_sql = '';

        //参数值数组
        $params_array = array();

        $return_data = array(
            'count' => 0,
            'data' => array()
        );

        $domain_url = ConfigureFunction::getConfigureField(43, 'domain_url');

        //品牌
        if (isset($arg_array['brandID']) && ebsig_is_int($arg_array['brandID'])) {
            $sql .= ' AND gm.brandID = ?';
            $params_array[] = $arg_array['brandID'];
        }

        //pc
        if (isset($arg_array['PC']) && ebsig_is_int($arg_array['PC'])) {
            $sql .= ' AND gm.PC = ?';
            $params_array[] = $arg_array['PC'];
        }

        //wap
        if (isset($arg_array['WAP']) && ebsig_is_int($arg_array['WAP'])) {
            $sql .= ' AND gm.WAP = ?';
            $params_array[] = $arg_array['WAP'];
        }

        //app
        if (isset($arg_array['APP']) && ebsig_is_int($arg_array['APP'])) {
            $sql .= ' AND gm.APP = ?';
            $params_array[] = $arg_array['APP'];
        }

        //按品牌筛选
        if ( isset($arg_array['brand']) && !empty($arg_array['brand']) ) {
            $str = '';
            foreach ( $arg_array['brand'] as $item ) {
                if (ebsig_is_int($item)) {
                    $str .= " OR  gm.brandID = $item";
                }
            }
            $str = ltrim(trim($str), 'OR ');
            $sql .= ' AND ' .'(' . $str . ')';
        }

        //分类
        if (isset($arg_array['bigCategoryID']) && ebsig_is_int($arg_array['bigCategoryID'])) {
            $category_sql .= ' AND bigCategoryID = ' . $arg_array['bigCategoryID'];
        }
        if (isset($arg_array['midCategoryID']) && ebsig_is_int($arg_array['midCategoryID'])) {
            $category_sql .= ' AND midCategoryID = ' . $arg_array['midCategoryID'];
        }
        if (isset($arg_array['smallCategoryID']) && ebsig_is_int($arg_array['smallCategoryID'])) {
            $category_sql .= ' AND smallCategoryID = ' . $arg_array['smallCategoryID'];
        }

        //销售状态
        $sql .= ' AND gm.forsale = 1 AND gm.useFlg = 1 AND wm.storeID = ' . $arg_array['shop_id'];
        $sql = ' WHERE ' . substr($sql, 4);

        if ($category_sql) {
            $category_sql = ' WHERE ' . substr($category_sql, 4);
        }

        //统计数量
        $count_sql = 'SELECT COUNT(gm.mainPostID) AS c FROM goods_master gm 
                      INNER JOIN ( SELECT DISTINCT mainPostID FROM goods_extend_category ' . $category_sql . ' ) AS gec ON gec.mainPostID = gm.mainPostID
                      INNER JOIN wms_goods_master wm ON wm.mainPostID = gm.mainPostID' . $sql;
        $count_array = DB::select($count_sql, $params_array);
        $return_data['count'] = $count_array[0]['c'];
        if ($return_data['count'] == 0) {
            return $return_data;
        }

        //排序
        if (isset($arg_array['sortname']) && !empty($arg_array['sortname'])) {

            if (!isset($arg_array['sortorder']) || !in_array($arg_array['sortorder'], array('ASC', 'DESC', 'asc', 'desc'))) {
                $arg_array['sortorder'] = 'ASC';
            } else {
                $arg_array['sortorder'] = strtoupper($arg_array['sortorder']);
            }
            $sql .= sprintf(' ORDER BY gm.%s %s', $arg_array['sortname'], $arg_array['sortorder']);

        }

        //分页
        if (!isset($arg_array['page']) || !ebsig_is_int($arg_array['page']))
            $arg_array['page'] = 1;
        if (!isset($arg_array['rp']) || !ebsig_is_int($arg_array['rp'])) {
            $arg_array['rp'] = 10;
        }
        $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

        //查询商品数据
        $search_sql = 'SELECT gm.* FROM goods_master gm
                       INNER JOIN ( SELECT DISTINCT mainPostID FROM goods_extend_category ' . $category_sql . ' ) AS gec ON gec.mainPostID = gm.mainPostID
                       INNER JOIN wms_goods_master wm ON wm.mainPostID = gm.mainPostID ' . $sql;

        $return_data['data'] = DB::select($search_sql, $params_array);

        foreach($return_data['data'] as &$goods) {

            $goods['enableSaleAmount'] = 0;

            $goods_sale_data = GoodsSale::select('product_code','postID','product_spec','virtual_id')->where(['mainPostID'=>$goods['mainPostID'],'forsale'=>1])->get()->toArray();
            foreach($goods_sale_data as &$sale_goods) {
                $goods['virtual_id'] = $sale_goods['virtual_id'];
                $enableSaleAmount = GoodsStore::search($sale_goods['product_code'], $sale_goods['product_spec'], $arg_array['shop_id']);
                if ( $enableSaleAmount > 0 ) {
                    $goods['enableSaleAmount'] = $enableSaleAmount;
                    $goods['postID'] = $sale_goods['postID'];
                    break;
                }
            }

            $goods['spec_num'] = count($goods_sale_data);
            $goods['link'] = set_goods_link($goods['postID']);
            $goods['wap_link'] = set_wap_goods_link($goods['postID']);
            $goods['img_link'] = $domain_url . $goods['goodsMainPic'];

            if ( $arg_array['shop_id'] > 0 ) {
                $goods_markting_strategy = GoodsMarktingStrategy::where(['shop_id' => $arg_array['shop_id'],'postID' => $goods['postID']])->first();
                if ($goods_markting_strategy) {
                    $goods_markting_strategy = $goods_markting_strategy->toArray();
                    $goods['marketPrice'] = $goods_markting_strategy['marketPrice'];
                    $goods['salePrice'] = $goods_markting_strategy['salePrice'];
                    $goods['salePoints'] = $goods_markting_strategy['salePoints'];
                }
            }

        }

        return $return_data;

    }


    /**
     * 根据关键词查询商品主表
     */
    public static function getListByKeyword( $arg_array )
    {

        //sql字符串
        $sql = '';

        //参数值数组
        $params_array = array();

        $return_data = array(
            'count' => 0,
            'data' => array()
        );

        $domain_url = ConfigureFunction::getConfigureField(43, 'domain_url');

        //检查关键词
        if (!isset($arg_array['keyword']) || empty($arg_array['keyword'])) {
            return $return_data;
        }
        $sql .= ' gm.goodsName LIKE ?';
        $params_array[] = '%' . $arg_array['keyword'] . '%';

        //品牌
        if (isset($arg_array['brandID']) && ebsig_is_int($arg_array['brandID'])) {
            $sql .= ' AND gm.brandID = ?';
            $params_array[] = $arg_array['brandID'];
        }

        //pc
        if (isset($arg_array['PC']) && ebsig_is_int($arg_array['PC'])) {
            $sql .= ' AND gm.PC = ?';
            $params_array[] = $arg_array['PC'];
        }

        //wap
        if (isset($arg_array['WAP']) && ebsig_is_int($arg_array['WAP'])) {
            $sql .= ' AND gm.WAP = ?';
            $params_array[] = $arg_array['WAP'];
        }

        //app
        if (isset($arg_array['APP']) && ebsig_is_int($arg_array['APP'])) {
            $sql .= ' AND gm.APP = ?';
            $params_array[] = $arg_array['APP'];
        }

        //销售状态
        $sql .= ' AND gm.forsale = 1 AND gm.useFlg = 1';
        $sql = ' WHERE ' . substr($sql, 4);

        //统计数量
        $count_sql = 'SELECT COUNT(gm.mainPostID) AS c FROM goods_master gm
                      INNER JOIN wms_goods_master wm ON wm.storeID = ' . $arg_array['shop_id'] . ' AND wm.mainPostID = gm.mainPostID' . $sql;
        $count_array = DB::select($count_sql, $params_array);
        $return_data['count'] = $count_array[0]['c'];
        if ($return_data['count'] == 0) {
            return $return_data;
        }

        //排序
        if (isset($arg_array['sortname']) && !empty($arg_array['sortname'])) {

            if (!isset($arg_array['sortorder']) || !in_array($arg_array['sortorder'], array('ASC', 'DESC', 'asc', 'desc'))) {
                $arg_array['sortorder'] = 'ASC';
            } else {
                $arg_array['sortorder'] = strtoupper($arg_array['sortorder']);
            }
            $sql .= sprintf(' ORDER BY gm.%s %s', $arg_array['sortname'], $arg_array['sortorder']);

        }

        //分页
        if (!isset($arg_array['page']) || !ebsig_is_int($arg_array['page']))
            $arg_array['page'] = 1;
        if (!isset($arg_array['rp']) || !ebsig_is_int($arg_array['rp'])) {
            $arg_array['rp'] = 10;
        }
        $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

        //查询商品数据
        $search_sql = 'SELECT gm.* FROM goods_master gm
                      INNER JOIN wms_goods_master wm ON wm.storeID = ' . $arg_array['shop_id'] . ' AND wm.mainPostID = gm.mainPostID ' . $sql;

        $return_data['data'] = DB::select($search_sql, $params_array);

        foreach($return_data['data'] as &$goods) {

            $goods['enableSaleAmount'] = 0;

            $goods_sale_data = GoodsSale::select('product_code','postID','product_spec','virtual_id')->where(['mainPostID'=>$goods['mainPostID'],'forsale'=>1])->get()->toArray();
            foreach($goods_sale_data as &$sale_goods) {
                $goods['virtual_id'] = $sale_goods['virtual_id'];
                $enableSaleAmount = GoodsStore::search($sale_goods['product_code'], $sale_goods['product_spec'], $arg_array['shop_id']);
                if ( $enableSaleAmount > 0 ) {
                    $goods['enableSaleAmount'] = $enableSaleAmount;
                    $goods['postID'] = $sale_goods['postID'];
                    break;
                }
            }

            $goods['spec_num'] = count($goods_sale_data);
            $goods['link'] = set_goods_link($goods['postID']);
            $goods['wap_link'] = set_wap_goods_link($goods['postID']);
            $goods['img_link'] = $domain_url . $goods['goodsMainPic'];

            if ( $arg_array['shop_id'] > 0 ) {
                $goods_markting_strategy = GoodsMarktingStrategy::where(['shop_id' => $arg_array['shop_id'],'postID' => $goods['postID']])->first();
                if ($goods_markting_strategy) {
                    $goods_markting_strategy = $goods_markting_strategy->toArray();
                    $goods['marketPrice'] = $goods_markting_strategy['marketPrice'];
                    $goods['salePrice'] = $goods_markting_strategy['salePrice'];
                    $goods['salePoints'] = $goods_markting_strategy['salePoints'];
                }
            }

        }

        return $return_data;

    }

}
